{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: JsonObjectConcept Extraction"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: JsonObjectConcept Extraction\n",
        "\n",
        "import os\n",
        "from pprint import pprint\n",
        "from typing import Literal\n",
        "\n",
        "from contextgem import Document, DocumentLLM, JsonObjectConcept\n",
        "\n",
        "\n",
        "# Define product information text\n",
        "product_text = \"\"\"\n",
        "Product: Smart Fitness Watch X7\n",
        "Price: $199.99\n",
        "Features: Heart rate monitoring, GPS tracking, Sleep analysis\n",
        "Battery Life: 5 days\n",
        "Water Resistance: IP68\n",
        "Available Colors: Black, Silver, Blue\n",
        "Customer Rating: 4.5/5\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document object from text\n",
        "doc = Document(raw_text=product_text)\n",
        "\n",
        "# Define a JsonObjectConcept with a structure for product information\n",
        "product_concept = JsonObjectConcept(\n",
        "    name=\"Product Information\",\n",
        "    description=\"Extract detailed product information including name, price, features, and specifications\",\n",
        "    structure={\n",
        "        \"name\": str,\n",
        "        \"price\": float,\n",
        "        \"features\": list[str],\n",
        "        \"specifications\": {\n",
        "            \"battery_life\": str,\n",
        "            \"water_resistance\": Literal[\"IP67\", \"IP68\", \"IPX7\", \"Not water resistant\"],\n",
        "        },\n",
        "        \"available_colors\": list[str],\n",
        "        \"customer_rating\": float,\n",
        "    },\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([product_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept from the document\n",
        "product_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the extracted structured data\n",
        "extracted_product = product_concept.extracted_items[0].value\n",
        "pprint(extracted_product)\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}